package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.ui.fragment.VideoListFragment;

/**
 * 精彩视频
 */
public class VideoListActivity extends BaseActivity {

    private String[] mTabTitles = new String[]{"热门推荐", "最新发布", "最近学习"};
    private Fragment[] mFragments = new Fragment[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_video_list;
    }

    @Override
    protected void initViews() {
        mTopBarDivider.setVisibility(View.VISIBLE);
        TabLayout tabLayout = findViewById(R.id.tab_layout);
        ViewPager2 viewPager2 = findViewById(R.id.view_pager2);
        viewPager2.setAdapter(new VpFragmentAdapter(getSupportFragmentManager(), getLifecycle(),
                mFragments, getIntent().getStringExtra("type")));
        viewPager2.setOffscreenPageLimit(mFragments.length - 1);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                tabLayout, viewPager2, (tab, position) -> tab.setText(mTabTitles[position]));
        tabLayoutMediator.attach();
    }

    @Override
    protected int getTitleStringRes() {
        String type = getIntent().getStringExtra("type");
        if (TextUtils.equals(type, "video"))
            return R.string.great_video;
        else if (TextUtils.equals(type, "audio"))
            return R.string.audio_learn;
        else
            return 0;
    }

    public static void actionStartVideo(Context context) {
        Intent intent = new Intent(context, VideoListActivity.class);
        intent.putExtra("type", "video");
        context.startActivity(intent);
    }

    public static void actionStartAudio(Context context) {
        Intent intent = new Intent(context, VideoListActivity.class);
        intent.putExtra("type", "audio");
        context.startActivity(intent);
    }

    private static class VpFragmentAdapter extends FragmentStateAdapter {

        private Fragment[] mFragments;
        private String mType;

        VpFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle,
                          Fragment[] fragments, String type) {
            super(fragmentManager, lifecycle);
            mFragments = fragments;
            mType = type;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            if (mFragments[position] == null)
                mFragments[position] = VideoListFragment.newInstance(position, mType);
            return mFragments[position];
        }

        @Override
        public int getItemCount() {
            return mFragments.length;
        }
    }
}
